﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MedicalManagementSystem.Models;
using MedicalManagementSystem.Utils;
using MedicalManagementSystem.Helpers;

namespace MedicalManagementSystem.Controllers
{
    public class AddressController : GenericController
    {
        //
        // GET: /Address/
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Address/Details/5
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Address/Create
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        public ActionResult Create(int patientId)
        {
            var address = new Address();
            address.Patient = Patient.GetByPK(patientId);
            return View(address);
        } 

        //
        // POST: /Address/Create
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        [HttpPost]
        public ActionResult Create(Address address, int patientId, int disrictId)
        {
            address.District = District.GetByPK(disrictId);
            address.Patient = Patient.GetByPK(patientId);
            try
            {
                DbContext.Instance.AddToAddress(address);
                DbContext.Instance.SaveChanges();
                return RedirectToAction("Details","Patient", new { id = patientId });
            }
            catch
            {
                ClearModelStateErrors();
                AddViolataionsToModelState(address.GetRuleViolations());
                return View(address);
            }
        }
        
        //
        // GET: /Address/Edit/5
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        public ActionResult Edit(int id)
        {
            return View(Address.GetByPK(id));
        }

        //
        // POST: /Address/Edit/5
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        [HttpPost]
        public ActionResult Edit(Address address, int patientId, int disrictId)
        {
            var original = Address.GetByPK(address.id);
            try
            {
                original.District = District.GetByPK(disrictId);
                DbContext.Instance.Address.ApplyCurrentValues(address);
                DbContext.Instance.SaveChanges();
                return RedirectToAction("Details", "Patient", new { id = patientId});
            }
            catch
            {
                ClearModelStateErrors();
                AddViolataionsToModelState(original.GetRuleViolations());
                return View(original);
            }
        }

        //
        // GET: /Address/Delete/5
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Address/Delete/5
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
